﻿#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

#include "vector.h"
namespace std
{
	void test_vector1()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);


		vector<double> v2;
		v2.push_back(1.1);
		v2.push_back(2.2);
		v2.push_back(3.3);


		vector<string> v3;
		v3.push_back("李白");
		v3.push_back("苏轼");


		vector<int> v4(10, 5);
		vector<string> v5(++v3.begin(), --v3.end());//迭代器区间初始化

		string s = "hello world";
		vector<char> v6(s.begin(), s.end());
	}

	void test_vector2()
	{
		//vector 的遍历
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);


		//1.下标+[]
		for (size_t i = 0; i < v.size(); ++i)
		{
			v[i] += 1;
			cout << v[i] << " ";
		}
		cout << endl;

		//2.迭代器
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			*it -= 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		//3.范围for
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

	}

	void test_vector3()
	{
		vector<int> v;
		cout << v.max_size() << endl; //整形的最大值/单个类型的大小

		//增容机制  -- VS下 1.5倍的增容  Linux下 2倍
		v.reserve(100);//开空间
		v.resize(100, 1);//开空间+初始化

		// string vector等都有一个特点,删除数据，一般都不会缩容
		v.resize(20);
		cout << v.size() << endl;
		cout << v.capacity() << endl;

		//慎用  意义：还回了一些字节
		v.shrink_to_fit();//缩容了
		cout << v.size() << endl;
		cout << v.capacity() << endl;
	}
	void test_vector4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		//不支持下标 -- 用迭代器
		v.insert(v.begin(), -1);
		v.insert(v.begin(), -2);
		v.insert(v.begin(), -3);

		//第三个位置插入 
		//越界了不行 只能在有效位置插入
		v.insert(v.begin() + 3, 300);


		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;


		//头删   也只能在有效位置插入
		v.erase(v.begin());
		v.erase(v.begin());
		v.erase(v.begin());
		for (auto e : v)
		{
			cout << e << " ";
		}

	}

	void test_vector5()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		//vector<int>::iterator pos = find(v.begin(), v.end(), 3);
		auto pos = find(v.begin(), v.end(), 3);
		if (pos != v.end())
		{
			cout << "找到了" << endl;
			//v.erase(pos);
		}
		else
		{
			cout << "没到了" << endl;
		}

		for (auto e : v)
		{
			cout << e << " ";
		}
	}

	void test_vector6()
	{
		vector<int> v;
		v.push_back(3);
		v.push_back(1);
		v.push_back(5);
		v.push_back(2);
		v.push_back(4);

		//默认升序
		sort(v.begin(), v.end());
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		//如果要排成降序,使用仿函数
		//sort(v.begin(), v.end(),greater<int>());

		greater<int> g;
		sort(v.begin(), v.end(), g);
		for (auto e : v)
		{
			cout << e << " ";
		}

	}

	//void test_vector7()
	//{
	//	std::vector<int> v;
	//	v.reserve(10);
	//	v.push_back(1);
	//	v.push_back(2);
	//	v.push_back(3);
	//	v.push_back(4);

	//	cout << v.size() << ":" << v.capacity() << endl;
	//	auto pos = find(v.begin(), v.end(), 2);
	//	if (pos != v.end())
	//	{
	//		v.insert(pos, 20);
	//	}
	//	//pos成了野指针
	//	cout << *pos << endl;
	//	*pos = 10;
	//	cout << *pos << endl;


	//}


	class Solution {
		string _numToStr[10] = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
	public:
		void _letterCombing(string digits, size_t di, string combineStr, vector<string>& retV)
		{
			if (di == digits.size())
			{
				retV.push_back(combineStr);
				return;
			}
			//取到数字字符转换成数字,再取到映射的字符串
			int num = digits[di] - '0';
			string str = _numToStr[num];

			for (auto ch : str)
			{
				_letterCombing(digits, di + 1, combineStr + ch, retV);
			}
		}
		vector<string> letterCombinations(string digits) {
			vector<string> retV;
			if (digits.empty())
				return retV;

			size_t i = 0;
			string str;
			_letterCombing(digits, i, str, retV);
			return retV;

		}
	};

	//迭代器失效
	void test_vector7()
	{
		std::vector<int> v;
		v.reserve(10);
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		cout << v.size() << ":" << v.capacity() << endl;
		auto pos = find(v.begin(), v.end(), 2);
		if (pos != v.end())
		{
			v.insert(pos, 20);
		}

		cout << *pos << endl;
		*pos = 10;
		cout << *pos << endl;
	}

	//删除所有的偶数
	void test_vector8()
	{
		std::vector<int> v;
		v.reserve(10);
		v.push_back(10);
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(10);
		auto it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{

				it = v.erase(it);
			}
			else
			{
				++it; 
			}
		}
		for (auto e : v)
		{
			cout << e <<" ";
		}
		cout << endl;
	}

	
}
 
//int main()
//{
//	//std::test_vector8();
//	//Solution().letterCombinations("23");
//	lxy::test_vector6();
//	return 0;
//}

//int main()
//{
//	vector<int> v;
//	size_t sz = v.capacity();
//	for (int i = 0; i < 100; ++i)
//	{
//		v.push_back(i);
//		if (sz != v.capacity())
//		{
//			sz = v.capacity();
//			cout << "capacity changed: " << sz << '\n';
//		}
//	}
//
//	return 0;
//}

//int main()
//{
//	int ar[] = { 1,2,3,4,0,5,6,7,8,9 };
//	int n = sizeof(ar) / sizeof(int);
//	vector<int> v(ar, ar + n);
//	vector<int>::iterator it = v.begin();
//	while (it != v.end())
//	{
//		if (*it != 0)
//			cout << *it;
//		else
//			v.erase(it);
//		it++;
//	}
//	return 0;
//}


//int main()
//{
//	vector<int> v{ 1,2,3,4 };
//	for (size_t i = 0; i < v.size(); ++i)
//		cout << v[i] << " ";
//	cout << endl;
//	auto it = v.begin();
//	cout << "Before expansion:vector.capacity():" << v.capacity() << endl;
//
//	v.reserve(100);
//	cout << "After expansion:vector.capacity():" << v.capacity() << endl;
//
//	while (it != v.end())
//	{
//		cout << *it << " ";
//		++it;
//	}
//	cout << endl;
//
//	return 0;
//}



//#include <iostream>
//#include <vector>
//#include <algorithm>
//using namespace std;
//
//int main()
//{
//	vector<int> v{ 1,2,3,4,5 };
//	vector<int>::iterator it = find(v.begin(), v.end(), 3);
//
//	v.erase(it);
//	cout << *it << endl;
//
//	while (it != v.end())
//	{
//		cout << *it << " ";
//		++it;
//	}
//	cout << endl;
//
//	return 0;
//}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	vector<int> v{ 1,2,3,4,5,6 };
	vector<int>::iterator it = v.begin();

	while (it != v.end())
	{
		if (*it % 2 == 0)
		{
			v.erase(it);
		}
		++it;
	}

	for (auto e : v)
	{
		cout << e << " ";
	}

	cout << endl;
	return 0;
}

